package com.huazi.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.commonutils.JwtUtils;
import com.huazi.eduorder.entity.TOrder;
import com.huazi.eduorder.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-30
 */
@RestController
@CrossOrigin
@RequestMapping("/eduorder/t-order")
public class TOrderController {
    @Autowired
    TOrderService orderService;
    //生成订单号(根据请求头获取用户信息)
    @PostMapping("createOrder/{courseId}")
    public CommentResult createOrder(@PathVariable String courseId, HttpServletRequest request){
        //创建订单，返回订单
        //创建一个list，用于判断是存在还是添加
        List<String> list=new ArrayList<>();
        TOrder order=orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request),list);
        if(order.getMemberId()==null||order.getCourseId()==null){
            return CommentResult.getError().data("order","订单生成出错");
        }
        return CommentResult.getOK().data("order",order).data("list",list);
    }
    //根据会员id查询所有订单信息
    @GetMapping("getOrderInfos/{id}")
    public CommentResult getOrderInfos(@PathVariable String id){
        TOrder order = orderService.getOne(new QueryWrapper<TOrder>().eq("member_id",id).eq("status",0).orderByDesc("gmt_create"));
        return CommentResult.getOK().data("order",order);
    }
    //根据课程id和会员id、并且以状态查询该课程是否存在订单
    @GetMapping("getOrderBuyInfo/{courseId}/{memberId}/{status}")
    public CommentResult getOrderBuyInfo(@PathVariable("courseId") String courseId,@PathVariable("memberId") String memberId,@PathVariable("status") Integer status){
        //判断是否存在该课程订单
        TOrder one = orderService.getOne(new QueryWrapper<TOrder>().eq("course_id", courseId).eq("member_id", memberId).eq("status", status));

        if(one!=null){

            return CommentResult.getOK().data("order",one);
        }
        return CommentResult.getError().data("order","");
    }
    ////根据根据会员id查询所有未支付的订单信息
    @GetMapping("getOrderBuys/{memberId}")
    public CommentResult getOrderBuys(@PathVariable("memberId") String memberId){
        //判断是否已经购买该课程
        List<TOrder> orders = orderService.list(new QueryWrapper<TOrder>().eq("member_id", memberId).eq("status", 0).orderByDesc("gmt_create"));
        if(orders!=null){
            Double sum =0.0;
            for (TOrder to: orders
            ) {
               sum=sum+Double.parseDouble(to.getTotalFee().toString());
            }
            return CommentResult.getOK().data("orders",orders).data("total",sum).data("orderNum",orders.size());
        }
        return CommentResult.getOK().data("orders",new ArrayList<>()).data("total",0).data("orderNum",0);
    }
    //根据id删除订单信息
    @GetMapping("deleteOrder/{id}")
    public CommentResult deleteOrder(@PathVariable("id") String id){
        orderService.removeById(id);
        return CommentResult.getOK();
    }
}

